package in.miraclesirius.arunachaltimes;

import in.miraclesirius.arunachaltimes.WordpressMainFragment.OnHeadlineSelectedListener;
import in.miraclesirius.arunachaltimes.slidingnavigation.NavDrawerItem;
import in.miraclesirius.arunachaltimes.slidingnavigation.NavDrawerListAdapter;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class WordpressMainActivity extends Activity implements
		OnHeadlineSelectedListener {

	public static final String FULL_STORY = "fullstory";
	public static final String SECTIONA_NAME = "sectionname";

	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;

//	private CharSequence mDrawerTitle;
//	private CharSequence mTitle;

	private ArrayList<NavDrawerItem> navDrawerItems;
	private NavDrawerListAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//mTitle = mDrawerTitle = getTitle();
		// load slide menu items
		Resources res = getResources();
		TypedArray navMenu = res.obtainTypedArray(R.array.nav_drawer_items_wordpress);

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

		navDrawerItems = new ArrayList<NavDrawerItem>();

		for (int i = 0; i < navMenu.length(); ++i) {
			String title = navMenu.getString(i);
			navDrawerItems.add(new NavDrawerItem(title));
		}
		navMenu.recycle();
		adapter = new NavDrawerListAdapter(getApplicationContext(),
				navDrawerItems);

		mDrawerList.setAdapter(adapter);
		// enabling action bar app icon and behaving it as toggle button
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, // nav menu toggle icon
				R.string.app_name, // nav drawer open - description for
									// accessibility
				R.string.app_name // nav drawer close - description for
									// accessibility
		) {
			public void onDrawerClosed(View view) {
				//getActionBar().setTitle(mTitle);
				// calling onPrepareOptionsMenu() to show action bar icons
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				//getActionBar().setTitle(mDrawerTitle);
				// calling onPrepareOptionsMenu() to hide action bar icons
				invalidateOptionsMenu();
			}
		};
		mDrawerLayout.setDrawerListener(mDrawerToggle);

		if (savedInstanceState == null) {
			// on first time display view for first nav item
			// displayView(0);
		}

		displayView(0);
		mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
	}

	private class SlideMenuClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// display view for selected nav drawer item
			displayView(position);
		}
	}

	private void displayView(int position) {
		// update the main content by replacing fragments
		String title = navDrawerItems.get(position).getTitle();
		Fragment fragment = new WordpressMainFragment();
		Bundle pigeon = new Bundle();
		switch (title) {
		case "Home":
			pigeon.putString(SECTIONA_NAME, "");
			break;
		case "State News":
			pigeon.putString(SECTIONA_NAME, "category/all-state-news/");
			break;
		case "National":
			pigeon.putString(SECTIONA_NAME, "category/all-india-news/");
			break;
		case "North East":
			pigeon.putString(SECTIONA_NAME, "category/north-east-news/");
			break;
		case "Sports":
			pigeon.putString(SECTIONA_NAME, "category/all-sports-news/");
			break;
		case "INFA":
			pigeon.putString(SECTIONA_NAME, "category/features-news/");
			break;	
		case "Ring Side View":
			pigeon.putString(SECTIONA_NAME, "category/by-tongam-rina/");
			break;	
		case "Editorial":
			pigeon.putString(SECTIONA_NAME, "category/editorial/");
			break;
		case "Readers Forum":
			pigeon.putString(SECTIONA_NAME, "category/reader-forum/");
			break;			
		case "Spark":
			pigeon.putString(SECTIONA_NAME, "category/for-young-mind/");
			break;
		default:
			break;
		}

		//pigeon.putString(PAGE_TITLE, title);
		fragment.setArguments(pigeon);
		
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction()
				.replace(R.id.frame_container, fragment).commit();
		// update selected item and title, then close the drawer
		mDrawerList.setItemChecked(position, true);
		mDrawerList.setSelection(position);
		setTitle(navDrawerItems.get(position).getTitle());
		mDrawerLayout.closeDrawer(mDrawerList);
	}

	@Override
	public void setTitle(CharSequence title) {
		//mTitle = title;
		getActionBar().setTitle(title);
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Pass any configuration change to the drawer toggls
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		return super.onOptionsItemSelected(item);

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onArticleSelected(String fullstoryUrl) {
		Intent intent = new Intent(this, WordpressFullStoryActivity.class);
		intent.putExtra(FULL_STORY, fullstoryUrl);
		startActivity(intent);

	}
}
